/**
 * @author xiaobyu
 * @created Jun 16, 2016
 */
package mdt.figure;

import org.tigris.gef.presentation.FigText;
import mdt.model.IMDTModel;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;

public class FigSingleLineText extends FigTextMDT {
    private static final long serialVersionUID = 1L;
		/**
     * The properties of 'owner' that this is interested in
     */
    private String[] properties;

    private void initialize() {
        setTabAction(FigText.END_EDITING);
        setReturnAction(FigText.END_EDITING);
        setFilled(false);
        setLineWidth(1);
        setTextColor(Color.black);
    }

    /**
     * Construct text fig
     *
     * @param owner owning UML element
     * @param bounds position and size
     */
    public FigSingleLineText(IMDTModel owner, Rectangle bounds) {
        super(bounds.x, bounds.y, bounds.width, bounds.height);
        setOwner(owner);
    }

    public FigSingleLineText(IMDTModel owner) {
        this(owner, new Rectangle(0, 0, 0, 0));
    }

    /**
     * TODO: This function attempts to optimize the more generic
     * code in the parent, which also works correctly in this case.
     * Is this a good idea?
     */
    /*
    @Override
    public Dimension getMinimumSize() {

        Dimension d = new Dimension();

        Font font = getFont();

        if (font == null) {
            return d;
        }
        int maxW = 0;
        int maxH = 0;
        if (getFontMetrics() == null) {
            maxH = font.getSize();
        } else {
            maxH = getFontMetrics().getHeight();
            maxW = getFontMetrics().stringWidth(getText());
        }

      // Now force minimum dimensions for the text:
        maxW = Math.max(maxW, MIN_TEXT_WIDTH);

      // Now add the areas around the text to return the complete size:
        int overallH = maxH + getTopMargin() + getBotMargin()
                + 2 * getLineWidth();
        int overallW = maxW + getLeftMargin() + getRightMargin()
                + 2 * getLineWidth();
        d.width = overallW;
        d.height = overallH;
        return d;
    }
    */

    @Override
    protected boolean isStartEditingKey(KeyEvent ke) {
        if ((ke.getModifiers() & (KeyEvent.META_MASK | KeyEvent.ALT_MASK)) == 0) {
            return super.isStartEditingKey(ke);
        } else {
            return false;
        }
    }


    @Override
    public void removeFromDiagram() {
        super.removeFromDiagram();
    }

    @Override
    public void propertyChange(PropertyChangeEvent pce) {
        if ("remove".equals(pce.getPropertyName())
                && (pce.getSource() == getOwner())) {
            deleteFromModel();
        }
    }


    /**
     * This function without parameter shall
     * determine the text of the Fig taking values from the owner,
     * and then call {@link #setText(String)}.
     * To be implemented as required by sub classes.
     */
    protected void setText() {
    }

    public void renderingChanged() {
      /* This is needed for e.g.
       * guillemet notation change on a class name,
       * see issue 5419. */
        setText();
    }

    public FigSingleLineText clone() {
        FigSingleLineText cloned = (FigSingleLineText)super.clone();
        if (properties != null)
            cloned.properties = properties.clone();
        return cloned;
    }
}
